package com.itheima.mongo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.dbservice.UserInfoServiceApi;
import com.itheima.mongoservice.VisitorsServiceApi;
import com.itheima.pojo.UserInfo;
import com.itheima.pojo.mongo.Visitors;
import com.itheima.vo.FriendPageList;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class VisitorsServiceApiImpl implements VisitorsServiceApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private UserInfoServiceApi userInfoServiceApi;
    @Override
    public List<Visitors> getVisitors(Long uid) {
        Query query = Query.query(Criteria.where("userId").is(uid));
        return mongoTemplate.find(query, Visitors.class);
    }

    @Override
    public void save(Visitors visitors) {
        mongoTemplate.save(visitors);
    }

    @Override
    public Visitors getVisitorInfo(Long uid, Long id) {
        Query query = Query.query(Criteria.where("userId").is(id)
                .and("visitorUserId").is(uid));
        return mongoTemplate.findOne(query, Visitors.class);
    }

    /*//谁看过我分页
    @Override
    public List<FriendPageList> visitorsPageList(Long uid, Integer page, Integer pagesize) {
        //构造条件
        Query query = Query.query(Criteria.where("userId")
                        .is(uid))
                .with(Sort.by(Sort.Order.desc("score")))
                .skip((page-1)*pagesize)
                .limit(pagesize);
        //
        List<Visitors> visitors = mongoTemplate.find(query, Visitors.class);
        //判断结果
        if (ObjectUtil.isNull(visitors)){
            return null;
        }
        ArrayList<FriendPageList> visitorPageList = new ArrayList<>();
        //不为空遍历集合
        for (Visitors visitor : visitors) {
            FriendPageList friendPageList = new FriendPageList();
            //获取参观者id
            Long visitorId = visitor.getVisitorUserId();
            //调用userInfoApi查询用户信息
            UserInfo userInfo = userInfoServiceApi.getUserInfoById(visitorId);
            //封装结果
            //这里不展示是否喜欢我们这里设置喜欢
            friendPageList.setAlreadyLove(true);
            BeanUtil.copyProperties(userInfo,friendPageList);
            friendPageList.setMatchRate(Convert.toInt(visitor.getScore()));
            visitorPageList.add(friendPageList);
        }
        //返回结果
        return visitorPageList;
    }*/


    //谁看过我分页
    @Override
    public List<FriendPageList> visitorsPageList(Long uid, Integer page, Integer pagesize,String nickname) {
        //构造条件
        Query query = new Query(Criteria.where("visitorUserId")
                .is(uid))
                .with(Sort.by(Sort.Order.desc("score")))
                .skip((page-1)*pagesize)
                .limit(pagesize);
        //
        List<Visitors> visitors = mongoTemplate.find(query, Visitors.class);
        //判断结果
        if (ObjectUtil.isNull(visitors)){
            return null;
        }
        //获取id集合
        List<Long> visitorUserIds = CollUtil.getFieldValues(visitors, "userId", Long.class);
        //查询用户详情
        List<UserInfo> userInfos = userInfoServiceApi.selectByIds(visitorUserIds, nickname);

        ArrayList<FriendPageList> visitorPageList = new ArrayList<>();
        //不为空遍历集合
        for (UserInfo userInfo : userInfos) {
            FriendPageList friendPageList = new FriendPageList();
            //获取参观者id
            Long id = userInfo.getId();
            //封装结果
            //这里不展示是否喜欢我们这里设置喜欢
            friendPageList.setAlreadyLove(true);
            BeanUtil.copyProperties(userInfo,friendPageList);
            friendPageList.setMatchRate(Convert.toInt(getVisitorInfo(uid,id).getScore()));
            visitorPageList.add(friendPageList);
        }
        //返回结果
        return visitorPageList;
    }

}
